package pox.workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipOutputStream;

import pox.channel.SharedStringWrite;
import pox.sheet.Sheet;
import pox.util.ZipUtil;

public class WorkBook {
	Map<String, Integer> workbookmap = new HashMap<String, Integer>();
	// Map<String, String> map=new HashMap<String, String>;
	Integer index = 0;
	static String PATH = "E:/poiExcel/test/";

	/***
	 * 初始化work,,开启内容写入线程
	 */
	public void init() {
		new Thread(new SharedStringWrite()).start();
	}

	public Sheet createSheet(String name) {
		index++;
		workbookmap.put(name, index);
		return new Sheet("sheet" + index);
	}

	public void write(FileOutputStream dstfile) throws IOException {
		writeWorkbook();
		writeRels();
		writeContentTypes();
		writeWorkRels();
		waitWritEnd();
		tozip(dstfile);
	}

	private void waitWritEnd() {
		while (!SharedStringWrite.isEnd()) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void tozip(FileOutputStream dstfile) throws IOException {
		ZipUtil zc = new ZipUtil();
		ZipOutputStream outputStream = new ZipOutputStream(dstfile);
		zc.compressDirectory(new File(PATH), outputStream, "");
		outputStream.close();
	}

	/**
	 * 加载sheet文件(xl/workbook.xml)
	 * 
	 * @param fileq
	 * @throws IOException
	 */
	private void writeWorkbook() throws IOException {
		File file = new File(PATH + "xl/workbook.xml");
		file.getParentFile().mkdirs();
		file.createNewFile();
		FileWriter fileOutputStream = new FileWriter(PATH + "xl/workbook.xml");
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"><fileVersion appName=\"xl\" lastEdited=\"5\" lowestEdited=\"5\" rupBuild=\"9303\"/><workbookPr filterPrivacy=\"1\" defaultThemeVersion=\"124226\"/><bookViews><workbookView xWindow=\"240\" yWindow=\"120\" windowWidth=\"16155\" windowHeight=\"8505\"/></bookViews><sheets>";
		// <sheet name="Sheet1" sheetId="1" r:id="rId1"/>
		fileOutputStream.write(head);
		Set<Entry<String, Integer>> set = workbookmap.entrySet();
		String body;
		for (Entry<String, Integer> entry : set) {
			body = "<sheet name=\"" + entry.getKey() + "\" sheetId=\"" + entry.getValue() + "\" r:id=\"rId"
					+ entry.getValue() + "\"/>";
			fileOutputStream.write(body);
		}
		String end = "</sheets><calcPr calcId=\"145621\"/></workbook>";
		fileOutputStream.write(end);
		fileOutputStream.flush();
		fileOutputStream.close();

	}

	/**
	 * 文件映射关系(xl/_rels/workbook.xml.rels)
	 * 
	 * @throws IOException
	 */
	private void writeRels() throws IOException {
		File file = new File(PATH + "xl/_rels/workbook.xml.rels");
		file.getParentFile().mkdirs();
		file.createNewFile();
		FileWriter fileOutputStream = new FileWriter(PATH + "xl/_rels/workbook.xml.rels");
		// workbook.xml.rels
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">";
		fileOutputStream.write(head);
		String body;
		Set<Entry<String, Integer>> set = workbookmap.entrySet();

		for (Entry<String, Integer> entry : set) {
			body = "<Relationship Id=\"rId" + entry.getValue()
					+ "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet\" Target=\"worksheets/sheet"
					+ entry.getValue() + ".xml\" />";
			fileOutputStream.write(body);
		}
		String end = "<Relationship Id=\"rId" + (set.size() + 1)
				+ "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings\" Target=\"sharedStrings.xml\"/></Relationships>";
		fileOutputStream.write(end);
		fileOutputStream.flush();
		fileOutputStream.close();
	}

	/**
	 * 文件映射关系(_rels\.rels)
	 * 
	 * @throws IOException
	 */
	private void writeWorkRels() throws IOException {
		File file = new File(PATH + "_rels/.rels");
		file.getParentFile().mkdirs();
		file.createNewFile();
		FileWriter fileOutputStream = new FileWriter(PATH + "_rels/.rels");
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/></Relationships>";
		fileOutputStream.write(head);
		fileOutputStream.flush();
		fileOutputStream.close();
	}

	/**
	 * 文件映射关系([Content_Types].xml)
	 * 
	 * @throws IOException
	 */
	private void writeContentTypes() throws IOException {
		File file = new File(PATH + "[Content_Types].xml");
		file.getParentFile().mkdirs();
		file.createNewFile();
		FileWriter fileOutputStream = new FileWriter(PATH + "[Content_Types].xml");
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/>";

		head += "<Override PartName=\"/xl/sharedStrings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml\"/>";
		head += "<Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/>";
		fileOutputStream.write(head);
		String body;
		Set<Entry<String, Integer>> set = workbookmap.entrySet();
		for (Entry<String, Integer> entry : set) {
			body = "<Override PartName=\"/xl/worksheets/sheet" + entry.getValue()
					+ ".xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>";
			fileOutputStream.write(body);
		}
		String end = "</Types>";
		fileOutputStream.write(end);
		fileOutputStream.flush();
		fileOutputStream.close();
	}
}
